package com.ruoyi.web.controller.aggregation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.domain.AjaxResult;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

/**
 * Redis测试Controller
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/agg/test")
public class RedisTestController
{
    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    /**
     * 测试Redis连接
     */
    @Anonymous
    @GetMapping("/redis")
    public AjaxResult testRedis()
    {
        Map<String, Object> result = new HashMap<>();
        
        try
        {
            // 测试写入
            String testKey = "test:key";
            String testValue = "test_value_" + System.currentTimeMillis();
            redisTemplate.opsForValue().set(testKey, testValue, 60, TimeUnit.SECONDS);
            result.put("写入测试", "✅ 成功");
            result.put("写入Key", testKey);
            result.put("写入Value", testValue);
            
            // 测试读取
            Object readValue = redisTemplate.opsForValue().get(testKey);
            result.put("读取测试", readValue != null && readValue.equals(testValue) ? "✅ 成功" : "❌ 失败");
            result.put("读取Value", readValue);
            
            // 查询所有agg:token:*
            Set<String> tokenKeys = redisTemplate.keys("agg:token:*");
            result.put("Token数量", tokenKeys != null ? tokenKeys.size() : 0);
            result.put("Token列表", tokenKeys);
            
            // 如果有token，读取一个看看
            if (tokenKeys != null && !tokenKeys.isEmpty())
            {
                String firstKey = tokenKeys.iterator().next();
                Object tokenValue = redisTemplate.opsForValue().get(firstKey);
                result.put("示例Token Key", firstKey);
                result.put("示例Token Value", tokenValue);
            }
            
            return AjaxResult.success(result);
        }
        catch (Exception e)
        {
            result.put("错误", e.getMessage());
            return AjaxResult.error("Redis测试失败: " + e.getMessage()).put("data", result);
        }
    }
    
    /**
     * 查看指定用户的Token
     */
    @Anonymous
    @GetMapping("/token")
    public AjaxResult checkToken(@RequestParam Long userId)
    {
        try
        {
            String key = "agg:token:" + userId;
            Object token = redisTemplate.opsForValue().get(key);
            
            Map<String, Object> result = new HashMap<>();
            result.put("userId", userId);
            result.put("redisKey", key);
            result.put("token", token);
            result.put("存在", token != null);
            
            return AjaxResult.success(result);
        }
        catch (Exception e)
        {
            return AjaxResult.error("查询失败: " + e.getMessage());
        }
    }
}

